////////////////////////////////////////////////////////////////////////////////
//(c) Intelliquip, Inc. 2001
////////////////////////////////////////////////////////////////////////////////
//$Log:
// 3    Version 10.0 (Current View)1.2         11/5/2010 1:05:29 PM   Stephen
//      Kish    IDD#6245: Refactoring changes
// 2    Version 10.0 (Current View)1.1         10/19/2010 2:26:29 PM  Stephen
//      Kish    IDD#6245: Refactoring HttpSessions.get and set Attribute to use
//      DAO classes
// 1    Version 10.0 (Current View)1.0         6/11/2010 5:20:57 PM   Eric
//      Zhang      
//$
//$NoKeywords$
////////////////////////////////////////////////////////////////////////////////
package com.intelliquip.hydraulicselection.control.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.intelliquip.core.servlet.IQServlet;
import com.intelliquip.hydraulicselection.UserPreferences;
import com.intelliquip.util.IEQMimeTypes;
import com.intelliquip.util.ServletUtils;

/**
 * @author Eric
 *
 */
public class PopupSubmit extends IQServlet {
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		SessionAttributeDAO_Selector dao = new HttpSessionAttributeDAO_Selector(session);
		UserPreferences userPreferences = dao.getUserPreferences();
		try {
			ServletUtils.setNoCache(response);
			response.setContentType(IEQMimeTypes.getHTMLContentType());
			if ( userPreferences == null){
				//forward to mainservlet to handle timeout
				String screenName = ScreenNames.SESSION_TIMEOUT_JSP;
				request.getRequestDispatcher(screenName).forward(request,response);
			} else {
				NavigationFlags navFlags = new NavigationFlags(request, session);
				String returnURL = navFlags.processCurrentScreen();

				//javascript to recalc parent window
				String recalcParentBrowser = request.getParameter("recalcParentBrowser");
				if(recalcParentBrowser!=null && recalcParentBrowser.equals("true")) {
					response.sendRedirect(request.getContextPath() + "/" + ScreenNames.RECALC_PARENT_BROWSER_HTM);
					return;
				//javascript to refresh current window
				} else {
					String currentPageUrl = request.getParameter("CurrentPage");
					UrlBuilder urlBuilder = new UrlBuilder(request, userPreferences, currentPageUrl);
					urlBuilder.setReturnURL(returnURL);  //this is necessary for popup submit jsp
					String target = urlBuilder.buildUrl(currentPageUrl, true);
					response.sendRedirect(target);
					return;
				}
			}
		} catch (Exception ex) {
			if (!response.isCommitted()) {
				request.setAttribute("javax.servlet.error.exception_type", ex);
				request.getRequestDispatcher("/errordisplay").forward(request, response);
				return;
			}
		}
	}

	/**
	 * Redirected to doGet().
	 */
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	// Clean up resources
	@Override
	public void destroy() {
	}
}
